相关框架:PrinterDispatch.framework
用于提供统一的BLE和 WiFi 外部接口
相关类:PTDispatcher
7123// 获取当前 Wifi 的路由信息4- (NSDictionary *)getRouterInfo;56// 通过 ip 地址连接 wifi7- (void)connectWifi:(NSString *)ip;
1、扫描蓝牙
51如何获取扫描出来的蓝牙设备:任选一种2代理方法:记得设置 BLE 代理。3- (void)didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *,id> *)advertisementData RSSI:(NSNumber *)RSSI;4Block:5- (void)scanBluetooth:(PTDispatchDiscoverPeripheralBlock)discoverBlock;
2、连接指定蓝牙
101如何获取连接状态:任选一种2注册通知:不推荐。PTDispatchModeChangeToBLE3代理方法:推荐。 记得设置 BLE 代理。4- (void)didConnectPeripheral:(CBPeripheral *)peripheral5Block:推荐。6- (void)connectBluetooth:(CBPeripheral *)peripheral7notify:(BOOL)notify8success:(PTDispatchConnectSuccessBlock)successBlock9failure:(PTDispatchConnectFailureBlock)failureBlock10canSendData:(PTDispatchCanSendDataBlock)canSendBlock
3、发送数据给打印机
61/** 单纯发送数据给打印机 */2- (void)sendData:(NSData *)data;3/** 发送数据,并等待打印机返回值 */4- (void)sendData:(NSData *)data returnData:(PTDispatchReceiveBlock)receiveBlock;5/** 发送数据,并等待成功发送结束,或者发送失败原因 */6- (void)sendData:(NSData *)data success:(PTDispatchSendSuccessBlock)successBlock disconnect:(PTDispatchDisconnectBlock)disconnectBlock;
4、断开连接
31/** 断开连接蓝牙 */2- (void)disConnectBluetooth;3- (void)disConnectBluetooth:(PTDispatchDisconnectBlock)disconnectBlock;
21// 取消当前连接(Wifi 或者 BLE)2- (void)disConnectCurrentLine;
121typedef NS_ENUM(NSInteger, PTDispatchMode) {23PTDispatchModeOffline = 0, //离线模式4PTDispatchModeBLE, //蓝牙4.0模式5PTDispatchModeWiFi, //WiFi 模式6};78// 当前通讯调度模式9@property(nonatomic,assign) NSInteger currentDispatchMode;1011// 当前连接的蓝牙12@property(strong,nonatomic,readwrite) CBPeripheral *bluetoothConnected;
- 同时支持协议和通知两种方式来获取连接状态。
- 使用协议比使用通知在代码结构上更优雅。
- 相关协议:PTDispatchProtocol
- 相关类:PTDispatcher
111@protocol PTDispatchProtocol <NSObject>23@optional4/** 每发现一个蓝牙设备调用一次 */5- (void)didDiscoverPeripheral:(CBPeripheral *)peripheral RSSI:(NSNumber *)RSSI;67/** 成功连接 BLE 或者观察通知:PTDispatchModeChangeToBLE */8- (void)didConnectPeripheral:(CBPeripheral *)peripheral;910/** 成功连接 WiFi 或者观察通知:PTDispatchModeChangeToWIFI */11- (void)didConnectWiFi:(NSString *)ip;
BLE 模式的特点:
- 功耗低。
- 连接速度很快,连接距离远(100米)。
- 相应的传输数据量小,比传统蓝牙传输的小很多,单次约能发送20-127字节。
综上,BLE 模式适合用来做遥控,遥控 LED 灯,窗帘,智能电视之类。智能手环和手机的数据同步,防丢器之类的东西。BLE 模式并不适合用来做较大数据量的传输使用。
1、创建 CBCentralManager 中心对象
2、central扫描外设 discover
3、central连接外设 connect
4、扫描外设中的服务和特征discover
- 获取外设的 services:
- 执行:
discoverServices- 成功后执行:
peripheral:didDiscoverServices委托方法
- 获取外设的 Characteristics,获取 Characteristics 的值:
- 执行
discoverCharacteristics:forService- 成功后执行:
peripheral:didDiscoverCharacteristicsForService:error委托方法
- 获取外设的Characteristics 的 Descriptor 和 Descriptor 的值:
- 读特征值:
readValueForCharacteristic- 读到后进入:
didUpdateValueForCharacteristic:error委托方法- 搜索 Characteristic:
discoverDescriptorsForCharacteristic- 搜到后进入:
didDiscoverDescriptorsForCharacteristic:error委托方法- 获取到 Descriptors 的值:
peripheral:didUpdateValueForDescriptor:errorDescriptors 是对 characteristic 的描述,一般是字符串
- 把数据写到 Characteristic:
writeCharacteristic:charactericstic:value- 读 RSSI,用通知的方式订阅数据等。
5、与外设做数据交互 explored interact
6、订阅 Characteristic 的通知:
notifyCharacteristic:characteristic取消通知:
cancelNotifyCharacteristic:characteristic7、断开连接 disconnect:
disconnnectPeripheral:peripheral
1、创建 AsyncSocket 对象
2、在 iOS 系统设置中连接 WiFi
3、获取当前 WiFi 的路由 IP 地址
4、连接当前 WiFi 的 IP,端口为9001:
connectToHost:onPort:withTimeout:error:5、成功连接打印机 WiFi,
onSocket:didConnectToHost:port:代理方法6、断开打印机 WiFi,
onSocketDidDisconnect:代理方法7、向打印机 WiFi 发送数据:
writeData:withTimeout:tag: